﻿
Namespace LightSwitchApplication

    Public Class TimeSheet

        Dim timeInterval As TimeSpan
        Private Sub TotalHours_Compute(ByRef result As Double)
            ' Set result to the desired field value

            If Not TimeStart = Nothing AndAlso Not TimeFinish = Nothing AndAlso If(Not Break = Nothing, TimeSpan.TryParse(Break, timeInterval), True) Then
                result = Math.Round((CDate(TimeFinish) - CDate(TimeStart)).TotalHours - If(Not Break = Nothing, TimeSpan.Parse(Break).TotalHours, 0), 2) + If(LeaveHours.HasValue, LeaveHours, 0)
            End If
        End Sub

        Private Sub TimeFinish_Validate(results As EntityValidationResultsBuilder)
            ' results.AddPropertyError("<Error-Message>")
            If Not TimeFinish Is Nothing Then
                Try
                    If Not TimeSpan.TryParse(TimeFinish, timeInterval) Then
                        TimeFinish = Convert.ToDateTime(TimeFinish).ToString("h:mm tt")
                    Else
                        TimeFinish = GetValidTime(TimeFinish)
                    End If

                    If Not TimeStart Is Nothing Then
                        If CDate(TimeStart) > CDate(TimeFinish) Then
                            results.AddPropertyError("Time Start should not be later than Time Finish.")
                        End If
                    End If
                Catch ex As Exception
                    results.AddPropertyError("Not a valid time.")
                End Try
            End If
        End Sub

        Private Sub TimeStart_Validate(results As EntityValidationResultsBuilder)
            ' results.AddPropertyError("<Error-Message>")

            If Not TimeStart Is Nothing Then
                Try
                    If Not TimeSpan.TryParse(TimeStart, timeInterval) Then
                        TimeStart = Convert.ToDateTime(TimeStart).ToString("h:mm tt")
                    Else
                        TimeStart = GetValidTime(TimeStart)
                    End If

                    If Not TimeFinish Is Nothing Then
                        If CDate(TimeStart) > CDate(TimeFinish) Then
                            results.AddPropertyError("Time Start should not be later than Time Finish.")
                        End If
                    End If
                Catch ex As Exception
                    results.AddPropertyError("Not a valid time.")
                End Try
            End If
            
        End Sub


        Private Function GetValidTime(ByVal atimestring As String) As String
            Try
                Dim dt As DateTime = DateTime.ParseExact(atimestring, "H:mm", Nothing)
                Return Convert.ToDateTime(atimestring).ToString("h:mm tt")
            Catch
                Return "12:00 AM"
            End Try
        End Function

        Private Sub OvertimeHours_Validate(results As EntityValidationResultsBuilder)
            ' results.AddPropertyError("<Error-Message>")
            If OvertimeHours > 0 Then
                If OvertimeHours > TotalHours - LightTimeConstants.workHoursPerDay Then
                    results.AddPropertyError("Overtime hours not valid.")
                End If
            End If
        End Sub

        Private Sub Approved_Changed()
            Me.ApprovedBy = Application.User.Name
        End Sub
    End Class

End Namespace
